#include<assert.h>

#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_mixer.h"
#include "GameBase.h"

BitmapObjectStatic::BitmapObjectStatic(int x, int y, char* pGraphicPath) : GameBase(x,y), m_Surface(0)
{
	// load .bmp
	SDL_Surface* pLoad = SDL_LoadBMP(pGraphicPath);
	assert(pLoad!=0);
	if(pLoad != NULL)
	{
		m_Surface = SDL_DisplayFormat(pLoad);
		SDL_FreeSurface(pLoad);
	}
}

BitmapObjectStatic::~BitmapObjectStatic()
{
	// free surface
	if (m_Surface)
	{
		SDL_FreeSurface(m_Surface);
	}
}

int BitmapObjectStatic::Draw(SDL_Surface* pWorldSurface)
{
	// apply surface at position m_x, m_y
	//Temporary rectangle to hold the offsets
    SDL_Rect offset;
    
    //Get the offsets
    offset.x = m_x;
    offset.y = m_y;
    
    //Blit the surface
    return SDL_BlitSurface( m_Surface, NULL, pWorldSurface, &offset );
}

int BitmapObjectStatic::Move(int x, int y)
{
	return 0;
}

GameBase* BitmapObjectStatic::Action(Event action,int inputX, int inputY)
{
	return 0;
}

BitmapObject::BitmapObject(int x, int y, char* pGraphicPath) : GameBase(x,y), m_Surface(0)
{
	// load .bmp
	SDL_Surface* pLoad = SDL_LoadBMP(pGraphicPath);
	assert(pLoad!=0);
	if(pLoad)
	{
		Uint32 colorkey = SDL_MapRGB(pLoad ->format, 0, 162, 232);
		SDL_SetColorKey( pLoad , SDL_SRCCOLORKEY, colorkey);

		m_Surface = SDL_DisplayFormat(pLoad);
		SDL_FreeSurface(pLoad);
	}
}

BitmapObject::~BitmapObject()
{
	// free surface
	if (m_Surface)
	{
		SDL_FreeSurface(m_Surface);
	}
}




int BitmapObject::Draw(SDL_Surface* pWorldSurface)
{
	// apply surface at position m_x, m_y
	//Temporary rectangle to hold the offsets
    SDL_Rect offset;
    
    //Get the offsets
    offset.x = m_x;
    offset.y = m_y;
    
    //Blit the surface
    return SDL_BlitSurface( m_Surface, NULL, pWorldSurface, &offset );
}

int BitmapObject::Move(int x, int y)
{
	m_x = x; // make a += if you want acceleration 
	m_y = y;
	return 0;
}

GameBase* BitmapObject::Action(Event action,int inputX, int inputY)
{
	return 0;
}


BitmapAnimatedObject::BitmapAnimatedObject(int x, int y, char* pGraphicPathName, int NumberFrames, int FrameDelay, bool loop,int Xmove, int Ymove, bool collisions ) : GameBase(x,y), m_CurrentFrame(0), m_FrameDelay(FrameDelay), m_Loop(loop), m_Xmove(Xmove), m_Ymove(Ymove), m_collisions(collisions), m_LastUpdate(0)
{
	// load .bmp series
	char FileName[512];

	for (int i = 0 ; i < NumberFrames ; i++)
	{
		sprintf(FileName,"%s%s%02i.bmp",RESOURCEPATH,pGraphicPathName,i);

		SDL_Surface* pLoad = SDL_LoadBMP(FileName);
		if (pLoad != 0)
		{
		Uint32 colorkey = SDL_MapRGB( pLoad->format, 0, 162, 232);
		SDL_SetColorKey( pLoad, SDL_SRCCOLORKEY, colorkey);

		m_SurfaceVector.push_back(SDL_DisplayFormat(pLoad));

		SDL_FreeSurface(pLoad);
		}
	}

	// init frame pacing
	m_LastUpdate = SDL_GetTicks();
}

BitmapAnimatedObject::~BitmapAnimatedObject()
{
	// free surfaces
	for(SurfaceVector::iterator iter = m_SurfaceVector.begin() ; iter != m_SurfaceVector.end() ; iter++)
	{
		SDL_FreeSurface(*iter);
		*iter = 0;
	}
}

int BitmapAnimatedObject::Draw(SDL_Surface* pWorldSurface)
{
	// apply surface at position m_x, m_y
	//Temporary rectangle to hold the offsets
    SDL_Rect offset;
    
    //Get the offsets
    offset.x = m_x;
    offset.y = m_y;
    
	int BlitReturn = 0;

    //Blit the surface
	if(m_Active)
	{	
		if( (m_LastUpdate + m_FrameDelay) < SDL_GetTicks())
		{
			m_LastUpdate = SDL_GetTicks(); 
			m_CurrentFrame++;

			// increment frame counter
			if (m_CurrentFrame == m_SurfaceVector.size())
			{
				if(m_Loop)
				{
					m_CurrentFrame = 0;
				}
				else
				{
					m_CurrentFrame = 0;
					m_Active = false;  
				}
			}
		}
		BlitReturn = SDL_BlitSurface( m_SurfaceVector[m_CurrentFrame], NULL, pWorldSurface, &offset );
	}

	return BlitReturn;
}

int BitmapAnimatedObject::Move(int x, int y)
{
	return 0;
}


GameBase* BitmapAnimatedObject::Action(Event action,int inputX, int inputY)
{
	m_x += m_Xmove;
	m_y += m_Ymove;

	if (m_collisions)
	{
		if (action == Fire)
		{
			// check for collisoin with the duck, if not than dont do anything
			if (inputX + 49 > m_x + 15 )
			{
				if (inputX + 49 < m_x + 85)
				{
					if (inputY + 53 > m_y + 25 )
					{
						if (inputY + 53 < m_y + 65)
						{
							return new BitmapAnimatedObject(m_x,m_y,"blam",4,500,false,0,0,false);
						}
					}
				}
			}
		}
	}

	return 0;
}

TextObjectStatic::TextObjectStatic(int x, int y, char* pFont, int TextSize) : GameBase(x,y), m_Font(0), m_TextSize(TextSize)
{
	// load .bmp
	m_Font = TTF_OpenFont(pFont, m_TextSize);
	assert(m_Font);

}

TextObjectStatic::~TextObjectStatic()
{
	// free surface
	TTF_CloseFont(m_Font);
}

int TextObjectStatic::Draw(SDL_Surface* pWorldSurface)
{
    return 0;
}

int TextObjectStatic::SetText(SDL_Surface* pWorldSurface, const char* Text , unsigned char Red, unsigned char Green, unsigned char Blue)
{
	int return_value = -1;
	// apply surface at position m_x, m_y
	//Temporary rectangle to hold the offsets
    SDL_Rect offset;
    //Get the offsets
    offset.x = m_x;
    offset.y = m_y;

	SDL_Color TextColor = { Red, Green, Blue};
    m_Surface = TTF_RenderText_Solid(m_Font, Text, TextColor);   
    //Blit the surface
    return_value = SDL_BlitSurface( m_Surface, NULL, pWorldSurface, &offset );
	SDL_FreeSurface(m_Surface);
	return return_value;
}

int TextObjectStatic::Move(int x, int y)
{
	return 0;
}

GameBase* TextObjectStatic::Action(Event action,int inputX, int inputY)
{
	return 0;
}
